package com.spzx.order.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author LOGIN
 * @title TestReceiver
 * @create 2025/7/31 11:12
 * @description
 */
@Slf4j
@Component
public class TestReceiver {

    /*
    * 监听消息
    *
    * */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_TEST,durable = "true"),
            key = MqConst.ROUTING_TEST
    )

    )
    public void test(String content, Message  message){

        System.out.println("接收到消息"   + message);
    }

    /*
    * 监听确认消息
    * */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_CONFIRM,durable = "true"),
            key = MqConst.ROUTING_CONFIRM
    ))
    public void confirm(String content, Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("接收确认消息：{}", content);

            // false 确认一个消息，true 批量确认
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
            channel.basicNack(deliveryTag,false,false);
        }
    }
}
